﻿#ifndef MONGOAGENT_H
#define MONGOAGENT_H

#include "mongodb/mongodber.h"
#include <mongocxx/gridfs/bucket.hpp>
#include <bsoncxx/builder/stream/document.hpp>
#include <bsoncxx/json.hpp>

using bsoncxx::builder::stream::close_array;
using bsoncxx::builder::stream::close_document;
using bsoncxx::builder::stream::document;
using bsoncxx::builder::stream::finalize;
using bsoncxx::builder::stream::open_array;
using bsoncxx::builder::stream::open_document;

#define MONGOAGENT MongoAgent::getInstance()

// Meyers' Singleton
class MongoAgent
{
public:
    static MongoAgent& getInstance(){
        static MongoAgent instance;
        return instance;
    }

public:
    bool getConnectStatus();
    bool reConnect(){
        mongoDBer.reConnect();
        return getConnectStatus();
    };

    mongocxx::client& getClient(){return  mongoDBer.getClient();}
    long count(std::string dbName,std::string collectionName,bsoncxx::document::view doc);
    void insert(std::string dbName,std::string collectionName,bsoncxx::document::view doc);
    void update(std::string dbName,std::string collectionName,
                bsoncxx::document::view filter,bsoncxx::document::view doc);
    void updateOne(std::string dbName, std::string collectionName,
                           bsoncxx::document::view filter,bsoncxx::document::view doc);
    void remove(std::string dbName,std::string collectionName,bsoncxx::document::view doc);
    mongocxx::gridfs::bucket&& getGridFS(std::string dbName);

private:
    MongoAgent(){};
    MongoAgent(const MongoAgent&)=delete ;
    MongoAgent& operator=(const MongoAgent&)=delete ;

    MongoDBer mongoDBer;
};

#endif // MONGOAGENT_H
